package cn.gameback.supervisionservices.Controller;

import cn.gameback.supervisionservices.dto.User.RoleTableDTO;
import cn.gameback.supervisionservices.entity.LeaderCrew;
import cn.gameback.supervisionservices.util.Result;
import cn.gameback.supervisionservices.vo.RequestVo.UserVo.*;
import cn.gameback.supervisionservices.entity.User;
import cn.gameback.supervisionservices.service.UserService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserService userService;

    // post 请求 分页查询用户列表
    @PostMapping("/pagedUserList")
    public ResponseEntity<?> getPagedUserList(@RequestBody UserQuery userQueryRequest) {
        Page<User> paging = new Page<>(userQueryRequest.getCurrent(), userQueryRequest.getpageSize());
        IPage<User> userPage = userService.getAllUsers(userQueryRequest);
        return ResponseEntity.ok(userPage);
    }

    // POST 请求 获取角色列表 如果角色是 教师 就获取教师列表 如果是 组长就获取组长列表 如果是组员就获取组员列表
    @PostMapping("/roleTable")
    public ResponseEntity<?> RoleTable(@RequestBody RoleQuery roleQueryRequest) {
        Result<List<RoleTableDTO>> result = userService.getRoleTable(roleQueryRequest);
        return result.getCode() == 200 ? ResponseEntity.ok(result.toJson()) : new ResponseEntity<>(result.toJson(), HttpStatus.valueOf(result.getCode()));
    }

    // POST 请求 用户登录
    @PostMapping("/login")
    public ResponseEntity<?> Login(@RequestBody LoginQuery loginQueryRequest) {
        Result<User> result = userService.loginFun(loginQueryRequest);

        return result.getCode() == 200 ? ResponseEntity.ok(result.toJson()) : new ResponseEntity<>(result.toJson(), HttpStatus.valueOf(result.getCode()));
    }

    // POST 用户注册
    @PostMapping("/register")
    public ResponseEntity<?> register(@RequestBody RegisterQuery registerQueryRequest) {
        Result<User> result = userService.registerFun(registerQueryRequest);
        return  result.getCode() == 200 ? ResponseEntity.ok(result.toJson()) : new ResponseEntity<>(result.toJson(), HttpStatus.valueOf(result.getCode()));
    }

    // 更改上级用户
    @PostMapping("/changeLeader")
    public ResponseEntity<?> updateLeader(@RequestBody LeaderCrewQuery query) {
        userService.updateUserLeader(query);
        return ResponseEntity.ok("更改成功");
    }
}